package com.whfc.fse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fse.dao.FseResumeAttachMapper;
import com.whfc.fse.dao.FseResumeMapper;
import com.whfc.fse.entity.FseResume;
import com.whfc.fse.param.FseResumeParam;
import com.whfc.fse.service.FseResumeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(interfaceClass = FseResumeService.class, version = "1.0.0")
public class FseResumeServiceImpl implements FseResumeService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FseResumeMapper fseResumeMapper;

    @Autowired
    private FseResumeAttachMapper fseResumeAttachMapper;

    @Override
    public void add(FseResumeParam param) throws BizException {
        logger.info("添加特种设备履历：{}", param);
        FseResume fseResume = new FseResume();
        Boolean flag = true;
        if (param.getResumeType().equals(1)) {
            fseResume = fseResumeMapper.selectInstAllByFseId(param.getFseId());
            if (fseResume != null) {
                flag = false;
                BeanUtils.copyProperties(param, fseResume);
                fseResumeMapper.updateByPrimaryKeySelective(fseResume);
                fseResumeAttachMapper.del(fseResume.getId());
            }
        }
        if (flag) {
            fseResume = new FseResume();
            BeanUtils.copyProperties(param, fseResume);
            fseResumeMapper.insertSelective(fseResume);
        }
        List<String> attachUrl = param.getAttachUrl();
        if (attachUrl != null && !attachUrl.isEmpty()) {
            fseResumeAttachMapper.insertAll(fseResume.getId(), attachUrl);
        }
    }

    @Override
    public void edit(FseResumeParam param) throws BizException {
        logger.info("修改特种设备履历：{}", param);
        FseResume fseResume = fseResumeMapper.selectByPrimaryKey(param.getResumeId());
        if (fseResume == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "需要修改的特种设备履历不存在");
        }
        BeanUtils.copyProperties(param, fseResume);
        fseResumeMapper.updateByPrimaryKeySelective(fseResume);
        fseResumeAttachMapper.del(fseResume.getId());
        List<String> attachUrl = param.getAttachUrl();
        if (attachUrl != null && !attachUrl.isEmpty()) {
            fseResumeAttachMapper.insertAll(fseResume.getId(), attachUrl);
        }
    }

    @Override
    public void del(Integer resumeId) throws BizException {
        logger.info("删除特种设备履历 resumeId：{}", resumeId);
        fseResumeMapper.del(resumeId);
        fseResumeAttachMapper.del(resumeId);
    }

    @Override
    public PageData<FseResumeParam> list(Integer fseId, Integer resumeType, Integer pageNum, Integer pageSize) throws BizException {
        logger.info("特种设备履历列表,fseId：{},resumeType：{},pageNum：{},pageSize：{}", fseId, resumeType, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<FseResumeParam> list = fseResumeMapper.selectByFseIdAndResumeType(fseId, resumeType);
        for (FseResumeParam param : list) {
            List<String> url = fseResumeAttachMapper.selectUrlByResumeId(param.getResumeId());
            param.setAttachUrl(url);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }
}
